package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.CollectMapper;
import com.ruoyi.system.domain.Collect;
import com.ruoyi.system.service.ICollectService;

/**
 * 用户收藏Service业务层处理
 *
 * @author AousVolad
 * @date 2024-06-27
 */
@Service
public class CollectServiceImpl implements ICollectService {
    @Autowired
    private CollectMapper collectMapper;

    /**
     * 查询用户收藏
     *
     * @param favoriteId 用户收藏主键
     * @return 用户收藏
     */
    @Override
    public Collect selectCollectByFavoriteId(Long favoriteId) {
        return collectMapper.selectCollectByFavoriteId(favoriteId);
    }

    /**
     * 查询用户收藏列表
     *
     * @param collect 用户收藏
     * @return 用户收藏
     */
    @Override
    public List<Collect> selectCollectList(Collect collect) {
        return collectMapper.selectCollectList(collect);
    }

    /**
     * 新增用户收藏
     *
     * @param collect 用户收藏
     * @return 结果
     */
    @Override
    public int insertCollect(Collect collect) {
        //  查询收藏记录 没查到则直接添加，查到则1->0  0->1
        Collect findCollect = collectMapper.selectCollect(collect);
        System.out.println(findCollect);

        if (findCollect != null  && findCollect.getDeletedFlag().equals("0")){
            findCollect.setDeletedFlag("1");
            return collectMapper.updateCollect(findCollect);

        }else if(findCollect != null  && findCollect.getDeletedFlag().equals("1")){
            findCollect.setDeletedFlag("0");
            return collectMapper.updateCollect(findCollect);
        }
        return collectMapper.insertCollect(collect);
    }

    /**
     * 修改用户收藏
     *
     * @param collect 用户收藏
     * @return 结果
     */
    @Override
    public int updateCollect(Collect collect) {
        return collectMapper.updateCollect(collect);
    }

    /**
     * 批量删除用户收藏
     *
     * @param favoriteIds 需要删除的用户收藏主键
     * @return 结果
     */
    @Override
    public int deleteCollectByFavoriteIds(Long[] favoriteIds) {
        return collectMapper.deleteCollectByFavoriteIds(favoriteIds);
    }

    /**
     * 删除用户收藏信息
     *
     * @param favoriteId 用户收藏主键
     * @return 结果
     */
    @Override
    public int deleteCollectByFavoriteId(Long favoriteId) {
        return collectMapper.deleteCollectByFavoriteId(favoriteId);
    }

    @Override
    public Collect getNewsCollect(Long userId, Long articleId, Long videosId) {
        Collect collect = new Collect();
        collect.setUserId(userId);
        collect.setArticleId(articleId);
        collect.setVideosId(videosId);
        return collectMapper.selectCollect(collect);
    }
}
